<template>
  <van-button
    :loading="loading"
    v-if="isFollowed"
    class="follow-btn"
    round
    size="small"
    @click="clickBtn"
    >已关注</van-button
  >
  <van-button
    :loading="loading"
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    @click="clickBtn"
    icon="plus"
    >关注</van-button
  >
</template>

<script>
import { followUser, unfollowUser } from '@/api/article.js'
export default {
  name: '',
  model: {
    prop: 'isFollowed',
    event: 'onChangeFollow'
  },
  props: {
    // 当前按钮关注状态
    isFollowed: {
      default: false,
      required: true,
      type: Boolean
    },
    // 关注/取消关注的 目标用户id
    userId: {
      required: true
    }
  },
  data() {
    return {
      loading: false
    }
  },
  methods: {
    // 点击事件处理函数----------------------
    async clickBtn() {
      // 开启 加载状态
      this.loading = true
      try {
        // 如果 已关注，则 进行 取消关注操作
        if (this.isFollowed) {
          await unfollowUser(this.userId)
        } else {
          // 如果 未关注，则 进行 关注操作
          await followUser(this.userId)
        }
        // 调用 父组件方法，修改 关注状态
        this.$emit('onChangeFollow', !this.isFollowed)
        // 提示消息
        this.$toast.success('操作成功~')
      } catch (error) {
        this.$toast.fail('操作失败，请重试')
      }
      // 关闭 加载状态
      this.loading = false
    }
  },
  created() {},
  computed: {},
  watch: {},
  components: {}
}
</script>

<style lang="less" scoped>
</style>
